﻿/* Copyright (c) 2011-2012, Zetatron Consulting
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of Zetatron Consulting nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WorldServer.DAO;
using AionCxxLibrary.Enums;
using AionCxxLibrary.Models;
using AionCxxLibrary.Utilities;
using WorldServer.Data;

namespace WorldServer.Models
{
    public delegate void EventAwareListAddEvent<T>(object sender, EventArgs<T> e);
    public delegate void EventAwareListRemoveEvent<T>(object sender, EventArgs<T> e);
    public delegate bool EventAwareListBeforeAddEvent<T>(object sender, EventArgs<T> e);

    public class EventAwareList<T> : IList<T>
    {
        public event EventAwareListAddEvent<T> OnAdd;
        public event EventAwareListRemoveEvent<T> OnRemove;
        public event EventAwareListBeforeAddEvent<T> BeforeAdd;

        List<T> list = new List<T>();

        int IList<T>.IndexOf(T item)
        {
            return list.IndexOf(item);
        }

        void IList<T>.Insert(int index, T item)
        {
            lock (this)
            {
                list.Insert(index, item);
            }
        }

        void IList<T>.RemoveAt(int index)
        {
            lock (this)
            {
                list.RemoveAt(index); 
            }
        }

        T IList<T>.this[int index]
        {
            get
            {
                lock (this)
                {
                    return list[index]; 
                }
            }
            set
            {
                lock (this)
                {
                    list[index] = value; 
                }
            }
        }
        
        public void Add(T item)
        {
            ((ICollection<T>)this).Add(item);
        }

        void ICollection<T>.Add(T item)
        {
            lock (this)
            {
                if (BeforeAdd != null)
                    if (!BeforeAdd(this, new EventArgs<T>(item)))
                        return;
                list.Add(item);
                if (OnAdd != null)
                    OnAdd(this, new EventArgs<T>(item)); 
            }
        }

        public void AddRange(IEnumerable<T> item)
        {
            foreach(T i in item)
            {
                Add(i);
            }
        }

        public void Remove(T item)
        {
            ((ICollection<T>)this).Remove(item);
        }

        bool ICollection<T>.Remove(T item)
        {
            lock (this)
            {
                bool result = list.Remove(item);
                if (OnRemove != null)
                    OnRemove(this, new EventArgs<T>(item));
                return result; 
            }
        }

        public void Clear()
        {
            ((ICollection<T>)this).Clear();
        }

        void ICollection<T>.Clear()
        {
            lock (this)
            {
                list.Clear(); 
            }
        }

        bool ICollection<T>.Contains(T item)
        {
            return list.Contains(item);
        }

        void ICollection<T>.CopyTo(T[] array, int arrayIndex)
        {
            lock (this)
            {
                list.CopyTo(array, arrayIndex); 
            }
        }

        int ICollection<T>.Count
        {
            get { return list.Count; }
        }

        public int Count
        {
            get { return list.Count; }
        }

        bool ICollection<T>.IsReadOnly
        {
            get { return false; }
        }

        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            lock (this)
            {
                return list.GetEnumerator(); 
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            lock (this)
            {
                return list.GetEnumerator(); 
            }
        }

        public T Find(Predicate<T> predicate)
        {
            return list.Find(predicate);
        }

        public List<T> FindAll(Predicate<T> predicate)
        {
            return list.FindAll(predicate);
        }

    }

    public class EventArgs<T> : EventArgs
    {
        public EventArgs(T value) { val = value; }
        T val;
        public T Value { get { return val; } }
    }


}
